package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.generator.entity.CommodityEntity;
import io.renren.modules.generator.service.CommodityService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 商品列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-04-05 12:40:56
 */
@RestController
@RequestMapping("generator/commodity")
@Api("商品列表")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 列表
     */
    @ApiOperation("商品列表")
    @GetMapping("/list")
    @RequiresPermissions("generator:commodity:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = commodityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("商品信息")
    @GetMapping("/info/{commodityId}")
    @RequiresPermissions("generator:commodity:info")
    public R info(@PathVariable("commodityId") String commodityId){
		CommodityEntity commodity = commodityService.getById(commodityId);

        return R.ok().put("commodity", commodity);
    }

    /**
     * 保存
     */
    @ApiOperation("新增商品信息")
    @PostMapping("/save")
    @RequiresPermissions("generator:commodity:save")
    public R save(@RequestBody CommodityEntity commodity){
		commodityService.save(commodity);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("更新商品信息")
    @PostMapping("/update")
    @RequiresPermissions("generator:commodity:update")
    public R update(@RequestBody CommodityEntity commodity){
		commodityService.updateById(commodity);

        return R.ok();
    }

    /**
     * 点赞商品
     */
    @ApiOperation("点赞商品信息")
    @PostMapping("/like")
    @RequiresPermissions("generator:commodity:update")
    public R like(@RequestBody CommodityEntity commodity){
        commodityService.like(commodity.getCommodityId());

        return R.ok();
    }

    /**
     * 讨厌商品
     */
    @ApiOperation("讨厌商品信息")
    @PostMapping("/dislike")
    @RequiresPermissions("generator:commodity:update")
    public R dislike(@RequestBody CommodityEntity commodity){
        commodityService.dislike(commodity.getCommodityId());

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品信息")
    @PostMapping("/delete")
    @RequiresPermissions("generator:commodity:delete")
    public R delete(@RequestBody Long[] commodityIds){
		commodityService.removeByIds(Arrays.asList(commodityIds));

        return R.ok();
    }

}
